$(function() {
	new Vue({
		el: "#editGoodApp",
		data: {
			goodId: "",
			fullscreenLoading: false,
			districtDefault: [],
			fileAvatarList: [],
			fileCarouselList: [],
			fileDetailList: [],
			parameterGroupsList: [],
			uploadUrl: "http://39.100.131.147:8888/goods/address/uploadFile",
			form: {},
			options: [],
			uploadForm: {},

		},
		created: function() {
			parent.vueData.$data.hreHtml = "../editGood/index.html";
			parent.vueData.activeIndex = "7";
			parent.vueData.getLeftNav("7")
			parent.vueData.$data.categoryIndex = 1
			this.goodId = localStorage.getItem("goodId");
			localStorage.removeItem("goodId");
			this.getGoodInfo()
			this.getAllCategory()

		},
		methods: {
			getAllCategory: function() {
				this.fullscreenLoading = true;
				let shopId = getShopId();
				axios({
					url: POSTURL.selectAllGoodCategory,
					method: 'post',
					data: {
						shopId: shopId,
						admin_grade:"1",
					}
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						for (let i = 0; i < response.data.result.length; i++) {
							let obj = response.data.result[i]
							let category = new Object();
							category.value = obj.name + "+" + obj.uid;
							category.label = obj.name;
							category.children = [];
							for (let j = 0; j < obj.secondary.length; j++) {
								let data = obj.secondary[j]
								let secondCategory = new Object();
								secondCategory.value = data.name + "+" + data.uid
								secondCategory.label = data.name
								category.children.push(secondCategory)
							}
							this.options.push(category)
						}
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			handleChange(data) {
				let s = []
				for (let i = 0; i < data.length; i++) {
					let obj = data[i];
					s.push(obj.split("+")[0])
				}
				this.form.categoryName = s.join('>')

			},
			getGoodInfo() {
				this.fullscreenLoading = true
				axios({
					url: POSTURL.selectGoodById,
					method: 'post',
					data: {
						commodityId: this.goodId,
						admin_grade:"1",
					}
				}).then(response => {
					this.fullscreenLoading = false
					if (response.data.code === 100) {
						console.log(response.data);
						this.fileCarouselList = [];
						this.fileDetailList = [];
						this.fileAvatarList = [];
						this.form = response.data.result.commodity;
						this.form.categoryName = response.data.result.commodity.category.name + '>' + response.data.result.commodity
							.category.category.name
						this.fileAvatarList.push({
							name: "商品封面",
							url: this.form.cover
						})
						for (let i = 0; i < response.data.result.photos.length; i++) {
							let url = response.data.result.photos[i]
							this.fileCarouselList.push({
								name: "轮播图",
								url: url
							})
						}
						for (let j = 0; j < response.data.result.details.length; j++) {
							let obj = response.data.result.details[j]
							this.fileDetailList.push({
								name: "商品详情图",
								url: obj
							})
						}
						this.parameterGroupsList = response.data.result.parameterGroups

					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			//商品封面
			handleAvatarSuccess(response, file, fileList) {
				if (response.code == 100) {
					this.form.cover = response.result
					this.fileAvatarList.push({
						name: "商品封面",
						url: response.result,
					})
				}
			},
			handleAvatarExceed(files, fileList) {
				this.$message.warning("商品封面只能上传一张图片")
			},
			//商品轮播图
			handleCarouselExceed(files, fileList) {
				this.$message.warning("商品封面最多能上传5张图片")
			},
			
			handleCarouselSuccess(response, file, fileList) {
				if (response.code == 100) {
					this.fileCarouselList.push({
						name: "轮播图",
						url: response.result,
					})
				}
			},
			//商品详情图
			handleDetailSuccess(response, file, fileList) {
				if (response.code == 100) {
					this.fileDetailList.push({
						name: "商品详情图",
						url: response.result,
					})
				}
			},
			handleDetailExceed(files, fileList) {
				this.$message.warning("商品详情最多能上传5张图片")
			},
			beforeAvaterRemove(file,fileList) {
				  let index = this.fileAvatarList.findIndex( fileItem => {
				       return fileItem.url === file.url
				   })
				  this.fileAvatarList.splice(index, 1)
				return true;
			},
			beforeCourseRemove(file, fileList) {
				  let index = this.fileCarouselList.findIndex( fileItem => {
				       return fileItem.url === file.url
				   })
				  this.fileCarouselList.splice(index, 1)
				return true;
			},
			beforeDetailRemove(file, fileList) {
				  let index = this.fileDetailList.findIndex( fileItem => {
				       return fileItem.url === file.url
				   })
				  this.fileDetailList.splice(index, 1)
				return true;
			},
			beforeAvatarUpload(file) {
				// 文件类型进行判断  
				const isJPG = file.type === "image/png";
				const isLt2M = file.size / 1024 / 1024 < 2;

				if (!isJPG) {
					this.$message.error("上传头像图片只能是 JPG 格式!");
				}
				if (!isLt2M) {
					this.$message.error("上传头像图片大小不能超过 2MB!");
				}
				return isJPG && isLt2M;
			},
			isNotNumber(inputData) {
				if (parseFloat(inputData).toString() == "NaN") {
					return false;
				} else {
					return true;
				}
			},
			//修改商品信息
			editGood() {
				if (stringEmpty(this.form.name)) {
					this.$message.error('商品名不能为空');
					return;
				}
				if (!this.isNotNumber(this.form.newPrice)) {
					this.$message.error('商品售价只能为数字');
					return;
				}
				if (!this.isNotNumber(this.form.oldPrice)) {
					this.$message.error('成本价只能为数字');
					return;
				}
				if (stringEmpty(this.form.inventory)) {
					this.$message.error('库存不能为空');
					return;
				}
				if (stringEmpty(this.form.inventoryWarning)) {
					this.$message.error('库存警告数量不能为空');
					return;
				}
				if (stringEmpty(this.form.unit)) {
					this.$message.error('商品单位不能为空');
					return;
				}
				if (this.fileAvatarList.length <= 0) {
					this.$message.error('商品封面不能为空');
					return;
				}
				if (this.fileCarouselList.length <= 0) {
					this.$message.error('商品轮播图不能为空');
					return;
				}
				
				if (this.fileDetailList.length <= 0) {
					this.$message.error('商品详情图不能为空');
					return;
				}
				
				this.uploadForm.name = this.form.name
				this.uploadForm.title = this.form.name
				this.uploadForm.newPrice = regYuanToFen(this.form.newPrice, 100)
				this.uploadForm.oldPrice = regYuanToFen(this.form.oldPrice, 100)
				this.uploadForm.inventory = this.form.inventory
				this.uploadForm.inventoryWarning = this.form.inventoryWarning
				this.uploadForm.unit = this.form.unit
				if (this.districtDefault.length > 0) {
					this.uploadForm.categoryId = this.districtDefault[1].split("+")[1]
				} else {
					this.uploadForm.categoryId = this.form.category.category.uid
				}
				let carouselList = [];
				this.fileCarouselList.forEach(e => {
					carouselList.push(e.url);
				})
				let detailList = [];
				this.fileDetailList.forEach(e => {
					detailList.push(e.url);
				})
				this.uploadForm.detail = detailList.join(',')
				this.uploadForm.photo = carouselList.join(',')
				this.uploadForm.cover = this.form.cover
				this.uploadForm.uid = this.goodId
				this.uploadForm.shopId = getShopId()
				this.uploadForm.admin_grade = "1"
				this.fullscreenLoading = true;
				
				axios({
					url: POSTURL.updateCommodity,
					method: 'post',
					data: this.uploadForm
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						this.$message({
							showClose: true,
							message: response.data.message,
							type: 'success'
						});
						window.history.back(-1)
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			}
		}
	})
})
